package com.mrlee.controller;

import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Module;
import com.mrlee.bean.Role;
import com.mrlee.service.RoleService;
import com.mrlee.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import java.util.List;

/**
 * Created by dllo on 18/3/9.
 */

@Controller
public class RoleController {
    @Resource
    private RoleService roleService;


    /**
     * 角色列表页
     * @return
     */
    @RequestMapping("rolelist")
    public String roleList(){
        return "/role/role_list";
    }

    /**
     * 角色添加页
     * @return
     */
    @RequestMapping("roleadd")
    public String roleAdd(){
        return "/role/role_add";
    }

    /**
     * 角色修改页
     * @param id
     * @return
     */
    @RequestMapping("roleupdate")
    public String roleUpdate(@RequestParam("roleId")String id){
        return "/role/role_modi";
    }

    /**
     * 查询全部角色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allrole")
    public AjaxResult selectAllRole(){
        return roleService.selectAllRole();
    }

    /**
     * 分页
     * @param pageNo 当前页
     * @param pageSize 每页显示信息数
     * @return
     */
    @RequestMapping(value = "rolepage")
    @ResponseBody
    public PageInfo<Role> roleByPage(@RequestParam(value = "pagenum",required = false)Integer pageNo,//当前页
                                       @RequestParam(value = "pagesize",required = false)Integer pageSize){
        return roleService.queryRoleByPage(pageNo,3);
    }

    /**
     * 添加角色
     * @param name 角色名
     * @param modules 权限数组
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addrole")
    public AjaxResult addRole(@RequestParam("name")String name,@RequestParam("module[]") List<Integer> modules){
        AjaxResult result = new AjaxResult();
        if (name.isEmpty()){
            result.setCode("0");
            result.setMessage("请输入名称");
        }
        Role role = new Role();
        role.setName(name);
        return roleService.addRole(role,modules);
    }

    /**
     * 修改角色
     * @param id 角色Id
     * @param name 角色名
     * @param modules 权限数组
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updaterole")
    public AjaxResult updateRole(@RequestParam("roleId")int id, @RequestParam("name")String name, @RequestParam("module[]") List<Integer> modules){

        Role role = new Role();
        role.setName(name);
        role.setRoleId(id);
        return roleService.updateRole(role,modules);
    }

    /**
     * 删除角色
     * @param roleId 角色Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "delrole")
    public AjaxResult delRole(@RequestParam("roleId")int roleId){
        return roleService.deleteRole(roleId);
    }

    /**
     * 根据角色Id查找角色
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectrole")
    public Role selectRoleById(@RequestParam("roleId")int roleId){
        return roleService.selectRoleById(roleId);
    }

    /**
     * 全部权限
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allmodule")
    public List<Module> selectAllModule(){
        return roleService.selectAllModule();
    }
}
